/**
 * 
 */
package nl.sportan.samenloop.adapter;

import java.util.ArrayList;

import nl.sportan.samenloop.R;
import nl.sportan.samenloop.communication.Result;
import nl.sportan.samenloop.communication.StreamingRSSReader;
import nl.sportan.samenloop.model.Message;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

/**
 * @author Bram Wiekens
 * 
 */
public class TwitterPageAdapter extends PagerAdapter {

	private Context context;
	private String[] urls;
	private Handler handler;
	private ArrayList<Message> messages;
	//private ListView list;
	//private View container;

	public TwitterPageAdapter(Context context) {
		this.context = context;
		urls = new String[4];
		handler = new Handler();

		urls[0] = context.getString(R.string.timeline_url);
		urls[1] = context.getString(R.string.hashtag_url);
		urls[2] = context.getString(R.string.hashtag_samenloop_url);
		urls[3] = context.getString(R.string.hashtag_kwf_url);
	}

	private View createListView(View pager, String url) {
		View container = (View) LayoutInflater.from(context).inflate(
				R.layout.twitter_item_view, ((ViewPager) pager), false);

		ListView list = (ListView) container.findViewById(R.id.twitterList);

		LayoutAnimationController fadeInAnimation = AnimationUtils
				.loadLayoutAnimation(context, R.drawable.leftin);

		list.setLayoutAnimation(fadeInAnimation);
		list.setOnItemLongClickListener(new TwitterClickListener());

		initReader(url, container, list);

		return container;
	}

	private class ResultHandler implements Result<ArrayList<Message>> {
		private ListView list;
		private View container;
		
		public ResultHandler(ListView list, View container){
			this.list = list;
			this.container = container;
		}
		
		public void onSucces(ArrayList<Message> result) {
			messages = result;
			handler.post(new ListUpdater(this.list, this.container));

		}

		public void onFailure(Throwable e) {
			// TODO Auto-generated method stub

		}

	}

	@Override
	public Object instantiateItem(View pager, int position) {
		View v = createListView(pager, urls[position]);
		((ViewPager) pager).addView(v, 0);

		return v;

	}

	@Override
	public void destroyItem(View pager, int position, Object view) {
		((ViewPager) pager).removeView((LinearLayout) view);
	}

	private void initReader(String url, View container, ListView list) {
		StreamingRSSReader reader = new StreamingRSSReader(url);
		reader.setResultHandler(new ResultHandler(list,container));

		Thread t = new Thread(reader);

		t.start();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.view.PagerAdapter#getCount()
	 */
	@Override
	public int getCount() {
		return urls.length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.view.PagerAdapter#isViewFromObject(android.view.View,
	 * java.lang.Object)
	 */
	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void finishUpdate(View view) {
	}

	@Override
	public void restoreState(Parcelable p, ClassLoader c) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View view) {
	}

	private class ListUpdater implements Runnable {

		private ListView list;
		private View container;

		public ListUpdater(ListView list, View container) {
			this.list = list;
			this.container = container;
		}

		public void run() {
			if (this.list != null) {
				this.list.setAdapter(new RssAdapter(context, messages));

				View v = this.container.findViewById(R.id.progress);
				Animation fadeOutAnimation = AnimationUtils.loadAnimation(
						context, android.R.anim.fade_out);
				v.startAnimation(fadeOutAnimation);
				v.setVisibility(View.GONE);

				Animation fadeInAnimation = AnimationUtils.loadAnimation(
						context, android.R.anim.fade_in);
				this.list.startAnimation(fadeInAnimation);
				this.list.setVisibility(View.VISIBLE);
			}
		}

	}

	private class TwitterClickListener implements OnItemLongClickListener {

		public boolean onItemLongClick(AdapterView<?> adapter, View view,
				int position, long id) {
			Message m = (Message) adapter.getItemAtPosition(position);

			Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
			shareIntent.setType("text/plain");
			shareIntent.putExtra(android.content.Intent.EXTRA_TITLE,
					m.getTitle());

			shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
					m.getTitle());
			shareIntent.putExtra(
					android.content.Intent.EXTRA_TEXT,
					m.getDescription() + "\n\n" + m.getDate() + "   "
							+ m.getLink());

			context.startActivity(Intent.createChooser(shareIntent,
					"Deel bericht"));

			return true;
		}

	}

}
